package com.tjetc.impl;

import com.tjetc.dao.AdminMapper;
import com.tjetc.domian.Admin;
import com.tjetc.service.AdminService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    private AdminMapper adminMapper= Db.getMapper(AdminMapper.class);
    private Result result = new Result();

    @Override
    public Result login(Admin admin) {
        String username = admin.getUsername();
        String password = admin.getPassword();
        Admin admin1 = adminMapper.findByUsername(username);
        if (admin1 == null){
            return result.setCode(500).setMsg("用户名不存在!");
        }else if (admin1.getPassword().equals(password)){
            return result.setCode(200).setMsg("登录成功!").setData(admin1);
        }else {
            return result.setCode(500).setMsg("密码错误!");
        }
    }

    @Override
    public Result add(Admin admin) {
        try {
            String username = admin.getUsername();
            Admin admin1 = adminMapper.findByUsername(username);
            if (admin1 != null) {
                return result.setCode(500).setMsg("用户名已存在!");
            }
            int rows = adminMapper.insertSelective(admin);
            return rows == 1 ? result.setCode(200).setMsg("添加成功!") : result.setCode(500).setMsg("添加失败!");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("添加失败!");
    }

    @Override
    public Result update(Admin admin) {
        try {
            if (admin == null || admin.getId() == null || admin.getUsername() == null) {
                return result.setCode(500).setMsg("参数非法!");
            }

            Admin admin2 = adminMapper.selectByPrimaryKey(admin.getId());
            if (admin2 == null) { // 检查用户是否存在
                return result.setCode(500).setMsg("用户不存在!");
            }

            if (!admin.getUsername().equals(admin2.getUsername())) { // 检查用户名是否修改
                Admin admin1 = adminMapper.findByUsername(admin.getUsername());
                if (admin1 != null) { // 检查新用户名是否已存在
                    return result.setCode(500).setMsg("用户名已存在!");
                }
            } else {
                return result.setCode(500).setMsg("修改失败!不能和更改前名字相同!");
            }

            int rows = adminMapper.updateByPrimaryKey(admin); // 更新用户信息
            return rows == 1 ? result.setCode(200).setMsg("修改成功!") : result.setCode(500).setMsg("修改失败!");
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈信息
            return result.setCode(500).setMsg("修改失败! " + e.getMessage()); // 返回具体的异常信息
        }
    }


   /* @Override
    public Result update(Admin admin) {
        try {
            Admin admin2 = adminMapper.selectByPrimaryKey(admin.getId());
            if (admin2.getUsername()== null){
                return result.setCode(500).setMsg("用户不存在!");
            }else {
                if (!admin.getUsername().equals(admin2.getUsername())) {
                    String username = admin.getUsername();
                    if(adminMapper.findByUsername(username)==null){
                        if (adminMapper.findByUsername(username).getUsername() != null) {
                            return result.setCode(500).setMsg("用户名已存在!");
                        }
                        int rows = adminMapper.updateByPrimaryKey(admin);//判空后
                        return rows == 1 ? result.setCode(200).setMsg("修改成功!") : result.setCode(500).setMsg("修改失败!");
                    }


                } return result.setCode(500).setMsg("修改失败!不能和更改前名字相同!");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return result.setCode(500).setMsg("修改失败!1");
        }
    }
*/
    @Override
    public List<Admin> list() {
        List<Admin> list = adminMapper.list();
        if (list != null){
            return list;
        }
        return  null;
    }

    @Override
    public List<Admin> findByUsernameLike(String username) {
        return adminMapper.findByUsernameLike(username);
    }

    @Override
    public Result delete(int id) {
        try {
            Admin admin = adminMapper.selectByPrimaryKey(id);
            if (admin == null){
                return result.setCode(500).setMsg("用户不存在!");
            }
            int rows = adminMapper.deleteByPrimaryKey(id);
            return rows == 1 ? result.setCode(200).setMsg("删除成功!") : result.setCode(500).setMsg("删除失败!");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("删除失败!");
    }


}
